package com.banshuibao.bsb_android.activity;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ScrollView;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.util.CameraUtil;
import com.banshuibao.bsb_android.util.MyEditText;
import com.banshuibao.bsb_android.util.XToast;
import com.xw.repo.BubbleSeekBar;

import java.util.List;

public class QuickConsultationActivity extends BaseActivity implements View.OnClickListener, View.OnTouchListener, CameraUtil.OnClickOkListener {

    private MyEditText myEditText;
    private TextView pic_count;
    private RecyclerView recyclerView;
    private TextView money;
    private BubbleSeekBar seekBar;
    private String content;
    private String price;
    private String[] images;

    //传图片
    LocalBroadcastManager localBroadcastManager;
    BroadcastReceiver bReceiver;
    IntentFilter iFilter;
    private CameraUtil cameraUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quick_consultation);
        iFilter = new IntentFilter();
        iFilter.addAction("shangAction");

        iFilter.setPriority(100);
        bReceiver = new PayActivity.shangBroadCast();
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        localBroadcastManager.registerReceiver(bReceiver, iFilter);
        initView();
    }

    //发布成功之后 清除前一条的信息
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 100 && resultCode == RESULT_OK){
            cameraUtil.clear();
            pic_count.setText("(0/3)");
            myEditText.setText("");
            seekBar.setProgress(15);
        }
    }

    private void initView() {
        setTitleText("快速咨询");
        TextView menu = (TextView) findViewById(R.id.menu);
        menu.setVisibility(View.VISIBLE);
        menu.setText("发布");
        menu.setOnClickListener(this);
        myEditText = (MyEditText) findViewById(R.id.myEdit);
        myEditText.setOnTouchListener(this);
        pic_count = (TextView) findViewById(R.id.pic_count);
        recyclerView = (RecyclerView) findViewById(R.id.list);
        cameraUtil = new CameraUtil(this,3);
        cameraUtil.setLayout(recyclerView);
        cameraUtil.setOnClickOkListener(this);
        money = (TextView) findViewById(R.id.money);
        price = money.getText().toString().trim();
        seekBar = (BubbleSeekBar) findViewById(R.id.seekBar1);
        seekBar.setOnProgressChangedListener(new BubbleSeekBar.OnProgressChangedListener() {
            @Override
            public void onProgressChanged(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {
                price = String.valueOf(progress);
                money.setText(String.valueOf(progress));
            }

            @Override
            public void getProgressOnActionUp(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {

            }

            @Override
            public void getProgressOnFinally(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat) {

            }
        });
        seekBar.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                view.getParent().requestDisallowInterceptTouchEvent(true);
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }
                return false;
            }
        });
        ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);
        scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                seekBar.correctOffsetWhenContainerOnScrolling();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.menu:
                content = myEditText.getText().toString().trim();
                if (!TextUtils.isEmpty(content)) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(this).setCancelable(false);  //先得到构造器
                    builder.setTitle("确认发布"); //设置标题
                    builder.setMessage("发布的问题请在 我的 -> 咨询 中查看"); //设置内容
                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { //设置确定按钮
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent();
                            intent.setAction("shangAction");
                            intent.putExtra("images", images);
//                    Log.e("TAG", "onClick: "+als );
                            localBroadcastManager.sendBroadcastSync(intent);
//                            IntentManager.goPayActivity(QuickConsultationActivity.this,content,price,1);
                            Intent intent1 = new Intent(context, PayActivity.class);
                            intent1.putExtra("flag", 1);
                            intent1.putExtra("content", content);
                            intent1.putExtra("price", price);
                            startActivityForResult(intent1,100);
                            dialog.dismiss();
                        }
                    });
                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { //设置取消按钮
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
                    //参数都设置完成了，创建并显示出来
                    builder.create().show();
                } else {
                    XToast.showToast(QuickConsultationActivity.this, "描述信息不能为空");
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        localBroadcastManager.unregisterReceiver(bReceiver);
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        getFouc();
        return false;
    }

    private void getFouc() {
        myEditText.setFocusable(true);
        myEditText.setFocusableInTouchMode(true);
        myEditText.requestFocus();
        myEditText.findFocus();
    }

    @Override
    public void gobackData(List<String> list) {
        if (list != null) {
            images = list.toArray(new String[list.size()]);
            pic_count.setText("(" + list.size() + "/3)");
        }
    }
}
